package scau.gsmg.misp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import scau.gsmg.misp.dao.IStatisticsDAO;
import scau.gsmg.misp.service.IStatisticsService;

@Service
public class StatisticsService implements IStatisticsService {
	
	private IStatisticsDAO statisticsDAO;
	private static final Logger logger = Logger.getLogger(StatisticsService.class);
	
	public IStatisticsDAO getStatisticsDAO() {
		return statisticsDAO;
	}
	@Resource
	public void setStatisticsDAO(IStatisticsDAO statisticsDAO) {
		this.statisticsDAO = statisticsDAO;
	}
	/**
	 * 月销售统计报表
	 */
	@Override
	public List getMonthStatistics(int year) throws Exception {
		// TODO Auto-generated method stub
		//如果没有输入数据则统计当前年
		logger.info(year);
		if(year==0){
			 Calendar now = Calendar.getInstance();
			 year = Integer.parseInt(""+now.get(Calendar.YEAR));
		}
		return statisticsDAO.getMonthStatistics(year);
	}
	/**
	 * 商品类别销售统计报表
	 */
	@Override
	public List getSaleStatistics(Date beginDate ,Date endDate) throws Exception {
		// TODO Auto-generated method stub
		logger.info(" "+beginDate+beginDate);
		if(beginDate==null&&endDate==null){
			beginDate = new Date();  
			String date = new SimpleDateFormat("yyyy-MM-dd").format(beginDate);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			beginDate=sdf.parse(date);
			endDate=sdf.parse(date); 
		}else if(beginDate==null&&endDate!=null){
			
		}else if(beginDate!=null&&endDate==null){
			endDate = new Date();
		}
		return statisticsDAO.getSaleStatistics(beginDate, endDate);
	}
	/**
	 * 月销售图形报表
	 */
	@Override
	public List getChartsData(int year) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = statisticsDAO.highcharts(year);
		List saleData = new ArrayList();//得到销售量集合
		int size = list.size();
		if(list.size()>0){
		    for(Map<String, Object> map:list){
		    	saleData.add((double)map.get("total"));
		    }
		}
		return saleData;
	}
	/**
	 * 得到横坐标数据
	 */
	@Override
	public List xAxis(int year) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = statisticsDAO.highcharts(year);
		List xAxis = new ArrayList();//横坐标的月份
		if(list.size()>0){
		    for(Map<String, Object> map:list){
		    	xAxis.add((int)map.get("month")+"月");
		    }
		}
		return xAxis;
	}
	
}
